import { addCourse, deleteCourse, updateCourse, queryCourse } from '../services/course';

export default {
  namespace: 'course',

  state: {
    courseList: [],
  },

  effects: {
    *fetchCourseList({ payload = {} }, { call, put }) {
      const response = yield call(queryCourse, payload);
      if (response.status) {
        yield put({
          type: 'saveCourseList',
          payload: response.data,
        });
      }
    },

    *updateCourseList({ payload = {} }, { call, put }) {
      const data = yield call(updateCourse, payload);
      if (data.status) {
        const dataLsit = yield call(queryCourse, {});
        yield put({
          type: 'saveCourseList',
          payload: dataLsit.data,
        });
      }
    },

    *deleteCourseList({ payload = {} }, { call, put }) {
      const data = yield call(deleteCourse, payload);
      if (data.status) {
        const dataLsit = yield call(queryCourse, {});
        yield put({
          type: 'saveCourseList',
          payload: dataLsit.data,
        });
      }
    },

    *addCourseList({ payload = {} }, { call, put }) {
      const data = yield call(addCourse, payload);
      if (data.status) {
        const dataLsit = yield call(queryCourse, {});
        yield put({
          type: 'saveCourseList',
          payload: dataLsit.data,
        });
      }
    },
  },

  reducers: {
    saveCourseList(state, { payload }) {
      return {
        ...state,
        courseList: payload,
      };
    },
  },
};
